﻿#include "ipinputcontrol.h"
#include "ui_ipinputcontrol.h"
#include <QDebug>
#include <QMessageBox>
#define outlog qDebug()
ipInputControl::ipInputControl(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ipInputControl)
{
    ui->setupUi(this);

    //设置IP地址校验,禁止输入非法字符
    QRegularExpression regExp("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");
    QValidator *validator = new QRegularExpressionValidator(regExp, this);
    ui->lineEdit->setValidator(validator);
    ui->lineEdit_2->setValidator(validator);
    ui->lineEdit_3->setValidator(validator);
    ui->lineEdit_4->setValidator(validator);

    //设置Tab键按下时的切换顺序
    QWidget::setTabOrder(ui->lineEdit, ui->lineEdit_2);
    QWidget::setTabOrder(ui->lineEdit_2, ui->lineEdit_3);
    QWidget::setTabOrder(ui->lineEdit_3, ui->lineEdit_4);
    QWidget::setTabOrder(ui->lineEdit_4, ui->lineEdit);

    //在窗体上为lineEdit安装事件过滤器
    ui->lineEdit->installEventFilter(this);
    ui->lineEdit_2->installEventFilter(this);
    ui->lineEdit_3->installEventFilter(this);
    ui->lineEdit_4->installEventFilter(this);

    //输入时做检查
    connect(ui->lineEdit, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
    connect(ui->lineEdit_2, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
    connect(ui->lineEdit_3, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
    connect(ui->lineEdit_4, &QLineEdit::textChanged, this, &ipInputControl::checkInput);
}

ipInputControl::~ipInputControl()
{
    delete ui;
}

//检查ip地址输入，若输入超过2位或2位数值大于25时，发送事件自动切换到下一个输入口
void ipInputControl::checkInput(const QString& text)
{
    if(ui->lineEdit_4->hasFocus())
    {
        return ;
    }
    if(text.length() > 2)
    {
        QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
        qApp->sendEvent(this, &myEvent);
    }
    else if(text.length() == 2)
    {
        int temp = text.toInt();
        if(temp > 25)
        {
            QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
            qApp->sendEvent(this, &myEvent);
        }
    }
}
//回车键可以对输入做检查
void ipInputControl::keyPressEvent(QKeyEvent* e)
{
    if(e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)
    {
        QString ipSub1 = ui->lineEdit->text();
        QString ipSub2 = ui->lineEdit_2->text();
        QString ipSub3 = ui->lineEdit_3->text();
        QString ipSub4 = ui->lineEdit_4->text();
        if(ipSub1.isEmpty() || ipSub2.isEmpty() || ipSub3.isEmpty() || ipSub4.isEmpty())
        {
            emit warningIP("ip address input error.");
            QMessageBox::information(nullptr, "warning", "ip address input error.");
            outlog<<"ip address input is error.";
        }
        else
        {
            QString ipString = ipSub1 + "." + ipSub2 + "." + ipSub3 + "." + ipSub4;
            outlog << "ipString: " << ipString;
        }
    }
}
bool ipInputControl::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::KeyPress)
    {
        QLineEdit *txt = (QLineEdit *)watched;
        if (txt == ui->lineEdit || txt == ui->lineEdit_2 || txt == ui->lineEdit_3 || txt == ui->lineEdit_4)
        {
            QKeyEvent *key = (QKeyEvent *)event;

            //如果当前按下了小数点则移动焦点到下一个输入框
            if (key->text() == ".")
            {
                if(ui->lineEdit->hasFocus())
                    ui->lineEdit_2->setFocus();
                else if(ui->lineEdit_2->hasFocus())
                    ui->lineEdit_3->setFocus();
                else if(ui->lineEdit_3->hasFocus())
                    ui->lineEdit_4->setFocus();
                else if(ui->lineEdit_4->hasFocus())
                    ui->lineEdit->setFocus();
            }

            //如果按下了退格键并且当前文本框已经没有了内容则焦点往前移
            if (key->key() == Qt::Key_Backspace)
            {
                if (txt->text().length() <= 1)
                {
                    if(ui->lineEdit_2->hasFocus())
                        ui->lineEdit->setFocus();
                    else if(ui->lineEdit_3->hasFocus())
                        ui->lineEdit_2->setFocus();
                    else if(ui->lineEdit_4->hasFocus())
                        ui->lineEdit_3->setFocus();
                }
            }
        }
    }
    return QWidget::eventFilter(watched, event);
}
//返回ip地址字符串，用于获取
QString ipInputControl::getIP() const
{
    QString ipSub1 = ui->lineEdit->text();
    QString ipSub2 = ui->lineEdit_2->text();
    QString ipSub3 = ui->lineEdit_3->text();
    QString ipSub4 = ui->lineEdit_4->text();
    if(ipSub1.isEmpty() || ipSub2.isEmpty() || ipSub3.isEmpty() || ipSub4.isEmpty())
    {
        return "";
    }
    return ipSub1 + "." + ipSub2 + "." + ipSub3 + "." + ipSub4;;
}
//从ip地址字符串设置到ui
void ipInputControl::setIP(const QString& text)
{
    QStringList ipList = text.split(".");
    if(ipList.size() != 4)
    {
        emit warningIP("ip address format is error.");
        QMessageBox::information(nullptr, "warning", "ip address format error.");
        outlog<<"ip address format is error.";
        return;
    }
    ui->lineEdit->setText(ipList.at(0));
    ui->lineEdit_2->setText(ipList.at(1));
    ui->lineEdit_3->setText(ipList.at(2));
    ui->lineEdit_4->setText(ipList.at(3));
}
